<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/css/home.css">
</head>
<body>
  <h2>我是home页</h2>
  <ul>
    <% for(var i=0;i<cates.length;i++) { %>
      <li>
        <h2>a字段：<%= cates[i].a %></h2>
        <p> 
           b字段：
           <%= cates[i].b %>
        </p>
        <p> 
          c字段：
          <%= cates[i].c %>
       </p>
       <button _id="<%= cates[i]._id %>"  class="del">删除</button>
       <button _id="<%= cates[i]._id %>"  class="update">更新</button>
      </li>
    <%}%>
  </ul>
  <button class="add">点击添加一条数据</button>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.9.0/jquery.js"></script>
  <script>
    $(function(){
      $('button.add').click(function(){
        $.ajax({
          url: '/addCate',
          type: 'POST',
          dataType: 'json',
          success: function(res){
            if(res.code === 0) {
              // 成功了
              alert(res.msg);
              history.go(0);
            }else{
              // 增加失败
              alert(res.msg);
            }
          }
        })
      })
      // 删除
      $('.del').click(function(){
        var id = $(this).attr("_id");
        if(confirm('您确认删除吗？')){
          $.ajax({
            url: '/delCate',
            type: 'POST',
            data: {
              id: id
            },
            dataType:'json',
            success: function(res){
              if(res.code === 0) {
                alert(res.msg);
                history.go(0)
              }else{
                alert(res.msg);
              }
            }
          })
        }
      })

      // 更新
      $('.update').click(function(){
        var id = $(this).attr("_id");
        if(confirm('您确认修改吗？')){
          $.ajax({
            url: '/upateCate',
            type: 'POST',
            data: {
              id: id,
              a: '我是a字段红红火火恍恍惚惚'
            },
            dataType:'json',
            success: function(res){
              if(res.code === 0) {
                alert(res.msg);
                history.go(0)
              }else{
                alert(res.msg);
              }
            }
          })
        }
      })
    })
  </script>
</body>
</html>